package com.example.project02.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.project02.common.Constants;
import com.example.project02.common.Result;
import com.example.project02.controller.dto.UserDTO;
import com.example.project02.entity.User;
import com.example.project02.service.UserService;
import com.example.project02.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
//    @Autowired
//    private UserMapper userMapper;
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result register(@RequestBody UserDTO userDTO){
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        if(StringUtils.isBlank(username)||StringUtils.isBlank(password)){
            return Result.error(Constants.CODE_400,"参数错误");
        }
        return Result.success(userService.register(userDTO));
    }

    @PostMapping("/login")
    public Result login(@RequestBody UserDTO userDTO){
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        if(StringUtils.isBlank(username)||StringUtils.isBlank(password)){
            return Result.error(Constants.CODE_400,"参数错误");
        }
        UserDTO dto= userService.login(userDTO);
        return Result.success(dto);
    }

    @PostMapping
    public Result save(@RequestBody User user){
        return Result.success(userService.saveOrUpdate(user));
    }

    @GetMapping
    public List<User> findAll(){
        return userService.list();
    }

    @GetMapping("/username/{username}")
    public Result findOne(@PathVariable String username){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return Result.success(userService.getOne(queryWrapper));
    }
//@GetMapping("/username/{username}")
//public Result findOne(@PathVariable String username){
//    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//    queryWrapper.eq("username",username);
//    User user = userService.getOne(queryWrapper);
//    System.out.println("User: " + user);
//
//    Map<String, Object> userData = new HashMap<>();
//    userData.put("username", user.getUsername());
//    userData.put("nickname", user.getNickname());
//    userData.put("email", user.getEmail());
//    userData.put("phone", user.getPhone());
//    userData.put("address", user.getAddress());
//
//    return Result.success(userData);
//}

    @DeleteMapping("/{id}")
    public boolean delect(@PathVariable Integer id){
        return userService.removeById(id);
    }

//    批量删除
    @PostMapping("/del/batch")
    public boolean delectBatch(@RequestBody List<Integer> ids){
        return userService.removeByIds(ids);
    }

//    @GetMapping("/page")
//    public Map<String, Object> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize,@RequestParam String username) {
//        pageNum = (pageNum - 1) * pageSize;
//        username="%"+username+"%";
//        List<User> data = userMapper.selectPage(pageNum, pageSize,username);
//        Integer total = userMapper.selectTotal(username);
//        Map<String, Object> res = new HashMap<>();
//        res.put("data", data);
//        res.put("total", total);
//        return res;
//    }
@GetMapping("/page")
public IPage<User> findPage(@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize,
                            @RequestParam(defaultValue = "") String username,
                            @RequestParam(defaultValue = "") String email,
                            @RequestParam(defaultValue = "") String address) {


    IPage<User> page=new Page<>(pageNum,pageSize);
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    if(!"".equals(username)){
        queryWrapper.like("username",username);
    }
    if(!"".equals(email)){
        queryWrapper.like("email",email);
    }
    if(!"".equals(address)){
        queryWrapper.like("address",address);
    }
    queryWrapper.orderByDesc("id");
    return userService.page(page,queryWrapper);
}

}
